package com.itcast.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.reggie.common.R;
import com.itcast.reggie.dto.SetmealDto;
import com.itcast.reggie.entity.Category;
import com.itcast.reggie.entity.Setmeal;
import com.itcast.reggie.entity.SetmealDish;
import com.itcast.reggie.service.CategoryService;
import com.itcast.reggie.service.SetmealDishService;
import com.itcast.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hehehe
 * @ClassName SetmealController
 * @Description TODO
 * @date 2022/12/1/001 20:17
 * @Version 1.0
 */

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    private R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, queryWrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtoList = records.stream().map((setmael) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmael, setmealDto);
            Category category = categoryService.getById(setmael.getCategoryId());
            if (category != null)
                setmealDto.setCategoryName(category.getName());

            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }


    /**
     * 保存套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    private R<String> save(@RequestBody SetmealDto setmealDto) {

        log.info(setmealDto.toString());

        setmealService.saveWithSetmaelDish(setmealDto);

        return R.success("套餐保存成功！");
    }

    /**
     * 修改页面回显信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    private R<SetmealDto> queryById(@PathVariable String id) {
        SetmealDto setmealDto = setmealService.getByIdWithSetmealDishs(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    private R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithSetmaelDish(setmealDto);
        return R.success("套餐修改成功");
    }

    @GetMapping("/list")
    private R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
